/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
import { categoryGet, jobBatch } from "@/api/pro";
import { RootState } from "@/store";
import { CategoryType } from "@/types/pro";
import { dataTree } from "@/utils/tool";
import { Button, Cascader, Form, Select } from "antd";
import React, { useEffect, useState } from "react";
import { useSelector } from "react-redux";
const { Option } = Select;
const layout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 14 },
};

const tailLayout = {
  wrapperCol: { offset: 8, span: 16 },
};

const JobPub: React.FC = () => {
  const [form] = Form.useForm();
  const { userInfo } = useSelector((state: RootState) => state.user);
  const [cateList, setCateList] = useState<Array<CategoryType>>([]);
  useEffect(() => {
    categoryGet().then((res) => {
      let tree = dataTree(res.data.results); //将数据包整理为tree格式
      setCateList(tree);
    });
  }, []);
  const onFinish = (values: any) => {
    let [cateLv1, cateLv2, cateLv3] = values.catagory;
    values.cateLv1 = cateLv1;
    values.cateLv2 = cateLv2;
    values.cateLv3 = cateLv3;
    values.companyId = userInfo?.objectId;
    values.companyName = userInfo?.name;
    console.log(values);
    jobBatch(values);
  };

  const onReset = () => {
    form.resetFields();
  };

  return (
    <Form {...layout} form={form} name="control-hooks" onFinish={onFinish}>
      <Form.Item name="catagory" label="分类名称" rules={[{ required: true }]}>
        <Cascader
          options={cateList}
          fieldNames={{ label: "name", value: "name" }}
        />
      </Form.Item>
      <Form.Item name="mockname" label="分类名称" rules={[{ required: true }]}>
        <Select>
          <Option value="h5">H5</Option>
          <Option value="ios">iOS</Option>
          <Option value="java">Java</Option>
          <Option value="other">Other</Option>
        </Select>
      </Form.Item>

      <Form.Item {...tailLayout}>
        <Button type="primary" htmlType="submit">
          提交
        </Button>
        <Button htmlType="button" onClick={onReset}>
          重置
        </Button>
      </Form.Item>
    </Form>
  );
};

export default JobPub;
